package cn.com.nes.site.modules.opportuntiy;

import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.mybatis.agent.crm.entity.Opportunity;
import cn.com.nes.mybatis.agent.project.entity.TProject;
import cn.com.nes.site.entity.em.crm.opportunity.OpportunityClassificationEnum;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityBO;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 政府终端商务项目业务处理
 */
@Slf4j
@Service
public class EndUserOpportunityHandle extends RetailOpportunityHandle {

    public EndUserOpportunityHandle() {
        log.debug("EndUserOpportunityHandle instance ....");
    }

    /**
     * 商务项目类型
     * @return
     */
    @Override
    public OpportunityClassificationEnum opportunityClassification() {
      return OpportunityClassificationEnum.END_USRE;
    }

    @Override
    protected void validateBusniess(OpportunityBO opportunityBO) throws Exception {
        if (StrUtil.isNotEmpty(opportunityBO.getRelateOpportunityUuid())) {
            // 校验行政区划
            List<String> coverageArea = crmOpportunityService.getCoverageArea(opportunityBO.getRelateOpportunityUuid());
            if (CollUtil.isEmpty(coverageArea)) {
                throw new BusinessException("当前的政府公共商务项目未设置覆盖范围，此商务项目不能关联到该项目！");
            }
            if (!coverageArea.contains(opportunityBO.getAreaCode())) {
                throw new BusinessException("行政区划不符合项目设置的覆盖范围！");
            }
        }
    }

    @Override
    protected void settingDefaultFields(OpportunityBO opportunityBO, Opportunity opportunity) {
        super.settingDefaultFields(opportunityBO, opportunity);
        log.debug("EndUserOpportunityHandle settingDefaultFields ....");

    }

    @Override
    protected void postProcessorForRelationshipHandle(Opportunity opportunity, OpportunityBO opportunityBO, TProject project) throws Exception {
        super.postProcessorForRelationshipHandle(opportunity, opportunityBO, project);
    }

}
